<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'校区列表'),
	array('op'=>'edit','title'=>'添加校区'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['area_id'])){
		$params['area_id']=intval($_RQ['area_id']);
		$return['params']['area_id']=$_RQ['area_id'];
	}
	if(isset($_RQ['type'])){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=''.intval($_RQ['type']);
	}
	if(intval($_RQ['page'])){
		$this->session('page',intval($_RQ['page']));
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','password','type','lianxiname','title','area_id','tel','username','address','status','mobile','pcd','logo');
	$list=pdo_getslice('jy_kexie_store',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
				$item['pcd']=json_decode($item['pcd'],true);
				$item['address']=$item['pcd']['province'].' '.$item['pcd']['city'].' '.$item['pcd']['district'].' '.$item['pcd']['street'].' '.$item['address'];
				$item['password']=base64_decode($item['password']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('total'=>$total,'tip'=>'','data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'area_id','title'=>'所属区域','type'=>'select','options'=>'areas'),
			array('name'=>'type','title'=>'校区类型','type'=>'radio','options'=>'storetype'),
			array('name'=>'title','title'=>'校区名称','type'=>'string'),
			array('name'=>'logo','title'=>'校区logo','type'=>'image'),
			array('name'=>'lianxiname','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'tel','title'=>'固话','type'=>'string'),
			array('name'=>'username','title'=>'账号','type'=>'string'),
			array('name'=>'password','title'=>'密码','type'=>'string'),
			array('name'=>'address','title'=>'地址','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('type'=>'href','action'=>'store_pai','title'=>'排课管理'),
		)),
		'search'=>array(
			array('name'=>'area_id','width'=>10,'title'=>'所属区域','type'=>'select','options'=>'areas'),
			array('name'=>'type','width'=>10,'title'=>'校区类型','type'=>'radio','options'=>'storetype'),
			array('name'=>'title','width'=>10,'title'=>'学校名称','type'=>'string'),
		)
	);
	$return['storetype']=$this->storetype;
	$return['areas']=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$this->session('store_id',$id);
	}else{
		//set_html('framess',$this->menustore());
		//$id=$this->session('store_id');
	}
	if($id){
		$item=pdo_get('jy_kexie_store',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['pcd']=json_decode($item['pcd']);
		$item['worktime']=json_decode($item['worktime'],true);
		$item['lat_lng']=json_decode($item['lat_lng'],true);
		$item['bannner']=json_decode($item['bannner'],true);
		$item['store_lesson']=json_decode($item['store_lesson'],true);
		unset($item['password']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回校区列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'校区名称','type'=>'string'),
					array('name'=>'logo','title'=>'校区LOGO','type'=>'image'),
					array('name'=>'area_id','title'=>'所属区域','type'=>'select','options'=>'areas'),
					array('name'=>'type','title'=>'校区类型','type'=>'radio','options'=>'storetype'),
					array('name'=>'lianxiname','title'=>'联系人','type'=>'string'),
					array('name'=>'mobile','title'=>'电话','type'=>'string'),
					array('name'=>'tel','title'=>'固话','type'=>'string'),
					array('name'=>'username','title'=>'账号','type'=>'string'),
					array('name'=>'password','title'=>'密码','type'=>'password'),
					array('name'=>'image','title'=>'校区背景图片','type'=>'image'),
					array('name'=>'lat_lng','title'=>'坐标','type'=>'lat_lng'),
					array('name'=>'address','title'=>'地址','type'=>'string'),
					array('name'=>'week_days','title'=>'上课时间','type'=>'radio','options'=>'week_days'),
					array('name'=>'xianlu','title'=>'乘车线路','type'=>'string'),
					array('type'=>'array','name'=>'bannner','add_tip'=>'添加一个幻灯片',
						'list'=>array(
							array('type'=>'image','name'=>'image','title'=>'幻灯片'),	
							array('type'=>'string','name'=>'url','title'=>'幻灯片url'),	
						),
						'details'=>array(
							array('type'=>'image','name'=>'image','title'=>'幻灯片'),	
							array('type'=>'string','name'=>'url','title'=>'幻灯片url'),
						)
					),
					array('name'=>'content','title'=>'校区介绍','type'=>'richtext'),
					array('name'=>'paixu','title'=>'排序','type'=>'string'),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				)
			)
			
		)
	);
	$return['week_days']=$this->week_days;
	$return['storetype']=$this->storetype;
	$return['areas']=pdo_getall('jy_kexie_area',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['xinqi']=array(array('id'=>'1','title'=>'周一'),array('id'=>'2','title'=>'周二'),array('id'=>'3','title'=>'周三'),array('id'=>'4','title'=>'周四'),array('id'=>'5','title'=>'周五'),array('id'=>'6','title'=>'周六'),array('id'=>'7','title'=>'周日'));
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$postdata['pcd']=ijson_encode($postdata['pcd']);
	$postdata['worktime']=ijson_encode($postdata['worktime']);
	$postdata['bannner']=ijson_encode($postdata['bannner']);
	$postdata['lat_lng']=ijson_encode($postdata['lat_lng']);
	$data=array_fields(array('week_days','type','lianxiname','area_id','mobile','tel','xianlu','username','password','title','logo','image','lat_lng','address','worktime','bannner','content','paixu','status','pcd'),$postdata);
	$id=intval($postdata['id']);
	if($data['password']){
		$data['password']=base64_encode($data['password']);
	}
	if($id){
		pdo_update('jy_kexie_store',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_kexie_store',$data);
	}
	exi('操作成功！','','//list/page='.$this->session('page'));
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['area_id'])){
		$params['area_id']=intval($_RQ['area_id']);
		$return['params']['area_id']=$_RQ['area_id'];
	}
	$lesson=pdo_getall('jy_kexie_store',$params);
	foreach($lesson as &$v){
		$v['starttime']=date('Y-m-d',$v['starttime']);
		$v['endtime']=date('Y-m-d',$v['endtime']);
		$v['createtime']=$v['createtime']?date('Y-m-d',$v['createtime']):'';
		$v['areatitle']=pdo_getcolumn('jy_kexie_area',array('id'=>$v['area_id']),'title');
		$v['typetitle']=$this->storetype[$v['type']];
		$v['password']=base64_decode($v['password']);
		$v['week_daysnew']=$this->week_days[$v['week_days']];
	}
	$header=array(
		'title'=>'学校名称',
		'lianxiname'=>'联系人',
		'mobile'=>'联系人电话',
		'tel'=>'固定电话',
		'username'=>'账号',
		'password'=>'密码',
		'typetitle'=>'学校类型',
		'areatitle'=>'学校区域',
		'address'=>'学校地址',
		'xianlu'=>'乘车路线',
		'week_daysnew'=>'上课时间',
		'createtime'=>'录入时间',
	);
	cfc('tools')->export2excel($header,$lesson,'学校表');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_kexie_store',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的校区！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_kexie_store',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}